//index.js
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
//获取应用实例
const app = getApp();
var first = true;
var jingdu = '';
var weidu = '';
var cinemaId = '';
var cinema = '';
var hotcinema = [];
var upComingcinema = [];
var cinemaName = '';
var isnowing = true;
var jijiangnowing = false;
//地区联动插件的使用
var model = require('../../model/model.js');
var show = false;
var item = {};
var imgUrls = []
var hasgetUpcoming = false;
var cinemapojo = '';

Page({
  data: {
    imgUrl: config.imgHost,
    userInfo: {},//用户信息
    hasUserInfo: false,//用户信息
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isnowing: isnowing,
    jijiangnowing: jijiangnowing,
    mode: 'aspectFit',
    indicatorDots: true,//是否出现焦点 
    autoplay: true,  //是否自动播放 
    interval: 3000,  //自动播放时间间隔 
    duration: 1000,  //滑动动画时间,
    imgUrls: [],//图片路径
    menulist: [],//菜单列表
    movielist: [],//电影列表
    isnow: true,//是不是正在上映
    yyname: undefined,
    yyname_bool: undefined,
    address: '',
    dyname: '',
    dyid: '',
    cityname: '乌鲁木齐',//默认城市
  },


  onShow: function () {
    console.log(app.globalData.cinemapojo.CinemaID)
    cinemaName = app.globalData.cinemapojo.CinemaName;
    if (cinemaName!==undefined){
      wx.setNavigationBarTitle({
        title: cinemaName,
      })
    }
   
    console.log("cinemaNmae" + app.globalData.cinemapojo.cinemaName)
    console.log(hasgetUpcoming)
    var yyname_bool = false;
    if (cinemaName !== '') {
      yyname_bool = true;
    }
    var that = this;
    //重新请求热映
    if (!first) {
      return req.GetAllCinemaHotFilms("", "", app.globalData.cinemapojo.CinemaID).then(res => {
        console.log(res)
        if (res.data.success == true) {
          that.setData({
            isnow: true
          })
          //请求成功
          cinema = res.data.cinema;
          cinemapojo = cinema;
          hotcinema = res.data.data;
          cinemaName = cinema.CinemaName;
          wx.setNavigationBarTitle({
            title: cinemaName,
          })
          cinemaId = cinema.CinemaID;
          for (let i = 0; i < hotcinema.length; i++) {

            hotcinema[i].SyncTime = hotcinema[i].SyncTime.split('T')[0]
            if (('' + hotcinema[i].UserScore).length == 1) {
              hotcinema[i].UserScore = hotcinema[i].UserScore + '.0'
            }
          }
          that.setData({ movielist: hotcinema, cinemaName, yyname_bool: true })
        }
      })

      that.setData({
        cinemaName,
        movielist: hotcinema,
        yyname: wx.getStorageSync("yyname"),
        yyname_bool: yyname_bool
      })
    }

  },
  //页面的初始化数据
  onLoad: function () {
    wx.showLoading({});
    cinemaId = wx.getStorageSync("CinemaCode");
    console.log(cinemaId);
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    var that = this;
    //轮播图部分

    //TODO 获取轮播图
    return req.SlideImgs().then((res) => {
      imgUrls = res.data.data;
      that.setData({ imgUrls })
    }).then(() => {
      //定位 
      wx.getLocation({
        success: function (res) {
          jingdu = res.longitude;
          weidu = res.latitude;
          //TODO 获取正在热映

          return req.GetAllCinemaHotFilms(jingdu, weidu, cinemaId).then(res => {
            wx.hideLoading();
            console.log(res)
            if (res.data.success == true) {
              that.setData({
                isnow: true
              })
              //请求成功
              cinema = res.data.cinema;
              app.globalData.cinemapojo = cinemapojo = cinema;
              hotcinema = res.data.data;
              cinemaName = cinema.CinemaName
              wx.setNavigationBarTitle({
                title: cinemaName,
              })
              cinemaId = cinema.CinemaID;
              for (let i = 0; i < hotcinema.length; i++) {

                hotcinema[i].SyncTime = hotcinema[i].SyncTime.split('T')[0]
                if (('' + hotcinema[i].UserScore).length == 1) {
                  hotcinema[i].UserScore = hotcinema[i].UserScore + '.0'
                }


              }
              that.setData({ movielist: hotcinema, cinemaName, yyname_bool: true })
            }
          })
        },
      })
    })

    //菜单部分
    var menulist = [
      {
        ID: 1,
        imgPath: '/resources/yuanh.png',
        menuval: '在线充值'
      },
      {
        ID: 2,
        imgPath: '/resources/yuanh.png',
        menuval: '扫码领券'
      },
      {
        ID: 3,
        imgPath: '/resources/yuanh.png',
        menuval: '会员二维码'
      },
      {
        ID: 4,
        imgPath: '/resources/yuanh.png',
        menuval: '优惠券'
      },
    ];

    //赋值
    that.setData({
      // imgUrls: imgUrls,//图片路径
      menulist: menulist,//菜单列表
    })


    //结束标志符号
  },
  /**得到用户信息**/
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //点击正在上映，得到正在上映的电影
  gochknowmovie: function () {
    var that = this;
    that.setData({
      isnowing: true,
      movielist: hotcinema,
      jijiangnowing: false,
      isnow: true
    })
    // that.getsearchmovie();
  },
  //点击即将上映，得到即将上映的电影
  gonochknowmovie: function () {
    var that = this;
    //TODO获取即将上映的电影
    that.setData({
      isnowing: false,
      jijiangnowing: true,
      isnow: false
    })

    if (!hasgetUpcoming) { //没获取过
      return req.GetAllCinemaUpcomingFilms(cinemaId).then(res => {
        console.log(res)
        hasgetUpcoming = true;
        upComingcinema = res.data.data;
        for (let i = 0; i < upComingcinema; i++) {
          upComingcinema[i].SyncTime = upComingcinema[i].SyncTime.split('T')[0]
        }
        this.setData({ movielist: upComingcinema })
      })
    } else {
      that.setData({
        isnow: false,
        movielist: upComingcinema
      })
    }


    // that.getsearchmovie();
  },

  // 选择影院
  ck_cy: function () {
    first =false;
    wx.navigateTo({
      url: '../yingyuan/index?jingdu=' + jingdu + '&&weidu=' + weidu + '',
    })
  },
  /**购票**/
  gobuy: function (e) {
    wx.showLoading({mask: true});
    if (app.globalData.cinemapojo !== '') {
      cinemapojo = app.globalData.cinemapojo;
    }
    wx.navigateTo({
      url: '../yingyuan/detail?pojo=' + JSON.stringify(e.currentTarget.dataset.pojo) + '&&cinemapojo=' + JSON.stringify(cinemapojo) + ''
    })
  },
  /**预售**/
  goyuebuy: function (e) {
    if (!wx.getStorageSync("yyid")) {
      var id = e.currentTarget.dataset.id;
      var name = e.currentTarget.dataset.name;
      var dyid = that.data.dyid;//电影id
      var dyname = that.data.dyname;//电影名称
      //设置背景色部分
      this.chkmovielist(id);
      wx.navigateTo({
        url: '../yingyuan/index?id=' + id + "&moviename=" + name
      })
    } else {

      wx.navigateTo({
        url: '../yingyuan/detail?yyid=' + wx.getStorageSync("yyid") + "&yyname=" + wx.getStorageSync("yyname") + "&dyname=" + wx.getStorageSync("dyname") + "&dyid=" + wx.getStorageSync("dyid") + '&address=' + wx.getStorageSync("address"),
      })
    }
  },
  /***想看***/
  gowansee: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    that.chkmovielist(id);
    //提交想看的数据
    //刷新页面
    // that.getsearchmovie();
  },

  /**搜索得到电影**/
  getsearchmovie: function () {
    var that = this;

    var isnow = that.data.isnow;
    var movielist = [];
    if (isnow) {
      movielist = [
        {
          id: 1,
          name: '黑豹',
          imgpath: '/resources/tu6.jpg',
          isimax: true,
          pingfen: '550个人想看',
          yanyuan: '主演: 查德维克·博斯曼',
          paichang: '上映日期: 2018-03-09',
          typeval: 1,//0：购票，1：预售，2：相看
          ischk: false,
        },
        {
          id: 2,
          name: '彼得兔',
          imgpath: '/resources/tu5.jpg',
          isimax: false,
          pingfen: '用户评分  8.0',
          yanyuan: '主演: 詹姆斯·柯登',
          paichang: '今日排片6场',
          typeval: 0,//0：购票，1：预售，2：相看
          ischk: false,
        },
        {
          id: 3,
          name: '闺蜜2',
          imgpath: '/resources/tu7.jpg',
          isimax: false,
          pingfen: '用户评分  8.0',
          yanyuan: '主演: 薛凯琪 / 陈意涵 / 张钧甯',
          paichang: '今日排片6场',
          typeval: 0,//0：购票，1：预售，2：相看
          ischk: false,
        },
        {
          id: 4,
          name: '彼得兔',
          imgpath: '/resources/tu5.jpg',
          isimax: false,
          pingfen: '用户评分  8.0',
          yanyuan: '主演: 詹姆斯·柯登',
          paichang: '今日排片6场',
          typeval: 0,//0：购票，1：预售，2：相看
          ischk: false,
        },
      ];
    } else {
      movielist = [
        {
          id: 1,
          name: '红海行动',
          imgpath: '/resources/tu4.jpg',
          isimax: true,
          pingfen: '550人想看',
          yanyuan: '张译 黄景瑜 海清 杜江等',
          paichang: '上映日期: 2018-03-09',
          typeval: 1,//0：购票，1：预售，2：相看
          ischk: false,
        },
        {
          id: 2,
          name: '比得兔',
          imgpath: '/resources/tu5.jpg',
          isimax: false,
          pingfen: '800个人想看',
          yanyuan: '王宝强 刘浩然  肖央',
          paichang: '上映日期:  2018-03-09',
          typeval: 2,//0：购票，1：预售，2：相看
          ischk: false,
        },
        {
          id: 3,
          name: '闺蜜2',
          imgpath: '/resources/tu6.jpg',
          isimax: false,
          pingfen: '300个人想看',
          yanyuan: '薛凯琪/陈意涵/张钧蜜',
          paichang: '上映日期  2018-03-09',
          typeval: 2,//0：购票，1：预售，2：相看
          ischk: false,
        },
      ];
    }

    //赋值
    that.setData({
      movielist: movielist,//电影列表
    })
    //结束标识符
  },
  //设置选中的背景色
  chkmovielist: function (movieid) {
    var that = this;
    var txtarry = [];
    for (var i = 0; i < that.data.movielist.length; i++) {
      if (movieid == that.data.movielist[i].id) {
        txtarry[i] = {
          id: that.data.movielist[i].id,
          name: that.data.movielist[i].name,
          imgpath: that.data.movielist[i].imgpath,
          isimax: that.data.movielist[i].isimax,
          pingfen: that.data.movielist[i].pingfen,
          yanyuan: that.data.movielist[i].yanyuan,
          paichang: that.data.movielist[i].paichang,
          typeval: that.data.movielist[i].typeval,//0：购票，1：预售，2：相看
          ischk: true,
        }
      } else {
        txtarry[i] = {
          id: that.data.movielist[i].id,
          name: that.data.movielist[i].name,
          imgpath: that.data.movielist[i].imgpath,
          isimax: that.data.movielist[i].isimax,
          pingfen: that.data.movielist[i].pingfen,
          yanyuan: that.data.movielist[i].yanyuan,
          paichang: that.data.movielist[i].paichang,
          typeval: that.data.movielist[i].typeval,//0：购票，1：预售，2：相看
          ischk: false,
        }
      }
    }
    that.setData({
      movielist: txtarry
    })
    //结束标识符
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function (e) {
    var that = this;
    //请求数据
    model.updateAreaData(that, 0, e);
  },
  //点击选择城市按钮显示picker-view
  translate: function (e) {
    model.animationEvents(this, 0, true, 400);
  },
  //隐藏picker-view
  hiddenFloatView: function (e) {
    model.animationEvents(this, 200, false, 400);
  },
  //滑动事件
  bindChange: function (e) {
    var that = this;
    //获取省市区三级的数据
    model.updateAreaData(that, 1, e);
    item = that.data.item;
    var zhixiashi = item.provinces[item.value[0]].name;

    if (zhixiashi == "北京市" || zhixiashi == "天津市" || zhixiashi == "上海市" || zhixiashi == "重庆市" || zhixiashi == "香港特别行政区" || zhixiashi == "澳门特别行政区") {
      that.setData({
        cityname: zhixiashi
      });
    } else {
      that.setData({
        cityname: item.citys[item.value[1]].name
      });
    }
  },
  onReachBottom: function () {
  },
  nono: function () { },
})
